#!/usr/bin/env python
import sys, os
from Tkinter import *
import tkFileDialog
import tkColorChooser


class SelectionPanel:
    def __init__(self, root, x, y, w, h):
        self.root = root
        
        #RESOURCES
        r = root.theme.resources
        self.rect = Rect(x, y, w, h)
        
        self.massImage = r["massLabel.png"][0]
        self.constraintImage = r["constraintLabel.png"][0]
        self.clipImage = r["clipLabel.png"][0]
        
        self.font = r["DejaVuSans.ttf"]
        
        #LIST
        self.mode = "Mass"
        self.selection = []
        self.offset = 20
        
    def update(self):
        pass
    
    def draw(self, surface):
        x, y = self.rect.topleft
        for obj in self.root.selection:
            if obj.__class__.__name__ == "Mass":
                self.drawMass(surface, obj, (x, y))
            if obj.__class__.__name__ == "Constraint":
                self.drawConstraint(surface, obj, (x, y))
            if obj.__class__.__name__ == "Clip":
                self.drawClip(surface, obj, (x, y))
            y += self.offset
    
    def drawMass(self, surface, element, pos):
        string1 = "x: " + str(element.x)[0:5]
        string2 = "y: " + str(element.y)[0:5]
        text1 = self.font.render(string1, True, (0,0,0))
        text2 = self.font.render(string2, True, (0,0,0))
        surface.blit(text1, (pos[0] + self.offset, pos[1]))
        surface.blit(text2, (pos[0] + self.offset, pos[1] + self.offset / 2))
        
        surface.blit(self.massImage, pos)
        
    def drawConstraint(self, surface, element, pos):
        string1 = "length: " + str(element.length)[0:5]
        text1 = self.font.render(string1, True, (0,0,0))
        surface.blit(text1, (pos[0] + 24, pos[1]))
        
        surface.blit(self.constraintImage, pos)
        
    def drawClip(self, surface, element, pos):
        string1 = "mag: " + str(element.variation)[0:5]
        string2 = "offset: " + str(element.offset)[0:5]
        text1 = self.font.render(string1, True, (0,0,0))
        text2 = self.font.render(string2, True, (0,0,0))
        surface.blit(text1, (pos[0] + 24, pos[1]))
        surface.blit(text2, (pos[0] + 24, pos[1] + 12))
        
        surface.blit(self.clipImage, pos)



class configApp:
    def __init__(self, master):
        self.frame = Frame(master)
        self.frame.pack()
        
        #CHECKS
        #World
        self.staticCollision = IntVar()
        self.staticCollisionButton= Checkbutton(self.frame, variable=self.staticCollision)
        self.staticCollisionButton.grid(row=0, column=0)
        self.staticCollisionLabel = Label(self.frame, text="Static Bodies collide")
        self.staticCollisionLabel.grid(row=0, column=1)
        
        self.dynamicCollision = IntVar()
        self.dynamicCollisionButton = Checkbutton(self.frame, variable=self.dynamicCollision)
        self.dynamicCollisionButton.grid(row=1, column=0)
        self.dynamicCollisionLabel = Label(self.frame, text="Dynamic Bodies collide")
        self.dynamicCollisionLabel.grid(row=1, column=1)
        
        self.fileNameEntry = Entry(self.frame)
        self.fileNameEntry.insert(0, "Choose a saveGame")
        self.fileNameEntry.grid(row=0, column=3)
        self.fileButton = Button(self.frame, text="Browse", command=self.browseForSaveGame)
        self.fileButton.grid(row=0, column=4)
        
        #Interface
        
        #Demo
        
        #START
        self.startButton = Button(self.frame, text="[Start]", command=self.startGame)
        self.startButton.grid(row=1, column=4)
    
    def browseForSaveGame(self):
        fileName = tkFileDialog.askopenfilename()
        self.fileNameEntry.delete(0, END)
        self.fileNameEntry.insert(0, fileName)
        print fileName + " prepared for loading"
    
    def startGame(self):
        self.frame.quit()
        
        configFileName = "config.py"
        file = open(configFileName, "w")
        
        file.write("def loadConfig(game):\n")
        
        fileName = self.fileNameEntry.get()
        if fileName != "Choose a saveGame" and fileName != "":
            file.write("    game.load('" + fileName + "')" + "\n\n")
        
        file.write("    game.world.collideOnStaticFlag = " + str(self.staticCollision.get()) + "\n")
        file.write("    game.world.collideOnDynamicFlag = " + str(self.dynamicCollision.get()) + "\n")



def startConfig():
    root = Tk()
    app = configApp(root)
    root.mainloop()

def startGame():
    sys.path.insert(1, "lib")
    from main import Main
    return Main()

def configureGame(game):
    #try:
    from config import loadConfig
    loadConfig(game)
    #except Exception, message:
        #print "The configuration couldn't be loaded:", message

def cleanUp():
    pygame.quit()
    try:
        os.remove(os.path.abspath("config.pyc"))
    except Exception, message:
        pass



if __name__ == '__main__':
    startConfig()
